/*
 * 业务数据处理函数文件
*/
import { isHasOwnPropertyObject } from '@/utils/index.js';
import Layout from '@/views/Layout';
// 路由
export function RouterData(data) {
  // console.log('接收的数据：', data);
  if (data.length > 0) {
    for (let i = 0; i < data.length; i++) {
      // console.log('具体数据打印：', data[i]);
      let meta = {};
      meta.title = data[i].menuTitle;
      meta.icon = data[i].menuIcon;
      meta.unique = data[i].unique;
      data[i].meta = meta;
      if (data[i].componentIden == 'Layout') {
        data[i].component = Layout;
      } else {
        let str = '';
        data[i].componentIden.indexOf('/') != -1 ? str = '' : str = '/index';
        data[i].component = () => import('@/views/'+ data[i].componentIden + str);
      }

      if (data[i].children && data[i].children.length > 0) {
        RouterData(data[i].children);
      }
    }
  }
  return data;
}

export function RouterNeat(data) {
  if (data.length > 0) {
    for (let i = 0; i < data.length; i++) {
      isHasOwnPropertyObject(data[i], 'menuTitle') && delete data[i].menuTitle;
      isHasOwnPropertyObject(data[i], 'menuIcon') && delete data[i].menuIcon;
      isHasOwnPropertyObject(data[i], 'componentIden') && delete data[i].componentIden;

      if (data[i].children && data[i].children.length > 0) {
        RouterNeat(data[i].children);
      }
    }
  }
  return data;
}

// 面包屑
let resultBack = [];
export function GetCrumbsLevel(unique) {
  resultBack = [];
  let uniqueArr = [];
  if (unique.length > 1) {
    let a1 = unique.split('-')[0];
    let a2 = unique.substring(0,unique.length - unique.indexOf('-') -1);
    uniqueArr.push(a1,a2,unique);
  }
  return Array.from(new Set(uniqueArr));
}
export function getCramusData(arr, uniqueArray) {
  for (let i = 0; i < arr.length; i++) {
    for (let var1 in uniqueArray) {
      if (arr[i].unique == uniqueArray[var1]) {
        let obj = {
          path: arr[i].path,
          name: arr[i].menuTitle,
          icon: arr[i].menuIcon,
          unique: arr[i].unique
        };
        resultBack.push(obj);
      }
    }
    if (arr[i].hasOwnProperty('children') && arr[i].children.length > 0) {
      getCramusData(arr[i].children, uniqueArray);
    }
  }
}

export function outputResult() {
  return resultBack;
}
